angular.module('mamApp').controller('CPExamineController', ['$scope', 'CPExamineManager', '$state', '$uibModal','ImsDialog', function ($scope, CPExamineManager, $state, $uibModal,ImsDialog) {
    $scope.total = 0;
    $scope.levels = [
        {key: "", value:"CP级别"},
        {key: 20, value: $scope.table[20]},
        {key: 21, value: $scope.table[21]},
        { key: 22, value: $scope.table[22]
    }];
    $scope.coopTypes = [
        {key: "", value:"合作方式"},
        {key: 30, value: $scope.table[30]},
        {key: 31, value: $scope.table[31]}
    ];

    $scope.reviewStatuses = [
        {key: "", value:"认证状态"},
        {key: 7, value: $scope.table[7]},
        {key: 8, value: $scope.table[8]},
        {key: 9,value: $scope.table[9]
    }];

    $scope.statuses = [
        {key: "", value:"功能状态"},
        {key: 3, value: $scope.table[3]},
        {key: 4, value: $scope.table[4]},
        {key: 2, value: $scope.table[2]
    }];

    $scope.fileNames = [{key: "", value: "查找字段"},
        {key: "cpId", value: "CP_ID"},
        {key: "nickName",value: "昵称"},
        {key: "userName", value: "账户名"}];

    $scope.search = {
        pageNo: $state.params.pageNo || 1,
        pageSize: $state.params.pageSize || 10,
        reviewStatus: $state.params.reviewStatus?parseInt( $state.params.reviewStatus):"",
        coopType: $state.params.coopType?parseInt( $state.params.coopType):"",
        status: $state.params.status?parseInt( $state.params.status):"",
        cplevel: $state.params.cplevel?parseInt( $state.params.cplevel):"",
        startDay: $state.params.startDay||null,
        endDay: $state.params.endDay||null,
        key: $state.params.key || "",
        value: $state.params.value || ""
    };

    $scope.findData = function () {
        $scope.search.pageNo=1;
        $state.go($state.current.name, $scope.search);
    }
    $scope.noPermission=false;
    CPExamineManager.CPExamineList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {

            if(result.data.c=="n"){
                $scope.noPermission=true;
            }else{
                $scope.noPermission=false;
                $scope.CPExamine = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            }

        })


    $scope.disable = function(CPExamine){
        ImsDialog.open({title:"CP中心",message:"禁用CP会使其无法上传专辑或节目，确定进行禁用？"}).then(function (a) {
            if(a=="ok"){
                var  cpId=CPExamine.cpId;
                CPExamineManager.CPStatus(cpId,{status:2}).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload()
                    }else{
                    }
                })
            }
        })
    }
    $scope.enable = function(CPExamine){
        ImsDialog.open({title:"CP中心",message:"您确定将CP的上传功能开启吗？"}).then(function (a) {
            if(a=="ok"){
                var  cpId=CPExamine.cpId;
                CPExamineManager.CPStatus(cpId,{status:4}).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload()
                    }else{
                    }
                })
            }
        })

    }

    $scope.searchClick=function (keyword) {
        $scope.search.pageNo=1;
        $scope.search.keyword=keyword;
        $state.go($state.current.name,$scope.search);
    }

    $scope.selectChange=function () {
        $scope.search.pageNo=1;
        $state.go($state.current.name,$scope.search);
    }

    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        perPageOptions: [10, 20, 30, 40, 50],
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)
        }
    };

    $scope.rangeDate = {
        startDate:$scope.search.startDay ? moment($scope.search.startDay).format('X'):null,
        endDate:$scope.search.endDay? moment($scope.search.endDay).format('X'):null,
        onSelected: function (data, scope) {  //时间选择确认事件
            $scope.search.startDay=data.startDate?moment(new Date(parseFloat(data.startDate)*1000)).format("YYYY-MM-DD"):null;
            $scope.search.endDay=data.endDate?moment(new Date(new Date(parseFloat(data.endDate)*1000))).format("YYYY-MM-DD"):null;
            $scope.search.pageNo=1;
            $state.go($state.current.name,$scope.search);

        }
    };

    $scope.allCheckChange=function (allChecked) {
        $scope.CPExamine.forEach(function (examine) {
            examine.checked=allChecked;
        })
    }

    $scope.itemCheckChange=function (album) {
        $scope.allChecked=true;
        $scope.CPExamine.forEach(function (examine) {
            if(!examine.checked){
                $scope.allChecked=false;

            }
        })
    }


    $scope.disableSelected=function(){
        var ids=[];
        $scope.CPExamine.forEach(function (examine) {
            if(examine.checked){
                ids.push(examine.cpId)
            }
        })
        if(ids.length==0){
            return false;
        }
        ImsDialog.open({title:"CP中心",message:"禁用CP会使其无法上传专辑或节目，确定进行禁用？"}).then(function (a) {
            if(a=="ok"){
                CPExamineManager.CPStatus(ids,{status:2}).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload()
                    }else{
                    }
                })
            }
        })
    }

    $scope.keyup=function (e,keyword) {
        if(e.key=="Enter"){
            $scope.search.pageNo=1;
            $scope.search.keyword = keyword;
            $scope.search.reviewStatus = $scope.search.reviewStatus == "null" ? null : $scope.search.reviewStatus;
            $state.go($state.current.name, $scope.search);
        }
    }




}])

